import Vue from 'vue'
import VueRouter from 'vue-router'
// 数据看板
import DataBoard from './dataBoard'
import MonitorBoard from './monitorBoard'
import pipeBoard from './pipeBoard'
import protectiveFence from './protectiveFence'
import Monitor from './monitor'
import Form from './form'
import Equip from './equip'
import EquipPublic from './equipPublic'
import Stock from './stock'
import DevOps from './devOps'
import Authority from './authority'
import BaseArchives from './baseArchives'
import Investigation from './investigation'
import MiniApp from './miniApp'
import System from './system'
import ERP from './ERP'
import pipeline from './pipeline'
import progress from "./progress";
import supervise from './supervise'

Vue.use(VueRouter)

// 所以新增菜单路由，存放在该数组中。会根据路由权限表自动挂载路由
/** ***********************************************************************************
 *  特殊参数说明
 * meta: routeMatch 当存在用路由 /login/admin这种方式通过param传参时，通过routeMatch来匹配路由
 *
 **************************************************************************************/
export const asyncRouterMap = [...MonitorBoard, ...protectiveFence, ...Form, ...Equip, ...Investigation, ...EquipPublic, ...Stock, ...DevOps, ...Authority, ...BaseArchives, ...System,
  ...Monitor,
  ...ERP,
  ...pipeline,
  ...progress,
  ...supervise,
  ...DataBoard,
  {
    path: '/patrol',
    name: 'patrol',
    meta: { name: '大排查大整治' },
    component: () => import(/* webpackChunkName: "sensor" */'@/views/form/patrol/patrol')
  },
  {
    path: '/patrol_board',
    name: 'patrol_board',
    meta: {name: '巡查整治统计'},
    component: () => import('@/views/form/patrol/board.vue')
  },
  {
    path: '/slope-status',
    meta: {name: '边坡动态'},
    name: 'slope-status',
    component: () => import(/* webpackChunkName: "slope-status" */'@/views/base_info/slope-status'),
  },
  {
    path: '/damage-status',
    meta: {name: '地灾动态'},
    name: 'damage-status',
    component: () => import(/* webpackChunkName: "slope-status" */'@/views/base_info/damage-status'),
  },
  {
    path: '/maintenanceWorks',
    name: 'maintenanceWorks',
    meta: {name: '养护工程'},
    component: () => import(/* webpackChunkName: "403" */'@/components/error/development')
  },
  {
    path: '/slopeDynamics',
    name: 'slopeDynamics',
    meta: {name: '边坡动态'},
    component: () => import(/* webpackChunkName: "403" */'@/components/error/development')
  },
  {
    path: '/export',
    meta: {name: '导出'},
    name: 'export',
    component: () => import('@/views/import_export/export'),
  },
  {
    path: '/import',
    meta: {name: '导入'},
    name: 'export',
    component: () => import('@/views/import_export/import'),
  },
  {
    path: '/download',
    meta: {name: '下载'},
    name: 'export',
    component: () => import('@/views/import_export/download'),
  },
  {
    path: '/ranking-list',
    name: 'ranking-list',
    meta: {name: '排行榜'},
    component: () => import(/* webpackChunkName: "road-mgmt" */'@/views/base_info/ranking-list')
  },
  {
    path: '/account-import',
    name: 'account-import',
    meta: {name: '快速导入'},
    component: () => import(/* webpackChunkName: "account-import" */'@/views/authority_mgmt/account-import')
  },
  {
    path: '/data-report',
    name: 'data-report',
    meta: {name: '数据报表'},
    component: () => import(/* webpackChunkName: "road-mgmt" */'@/views/data_board/dataReport')
  },
  {
    path: '/damage-report',
    name: 'damage-report',
    meta: {name: '地灾报表'},
    component: () => import(/* webpackChunkName: "road-mgmt" */'@/views/data_board/damageReport')
  },
  {
    path: '/taizhang',
    name: 'taizhang',
    meta: {name: '地质灾害管理台账'},
    component: () => import(/* webpackChunkName: "road-mgmt" */'@/views/data_board/taiZhang')
  },
]

// 白名单路由，即不再权限控制范围内的路由
const constantRouterMap = [
  pipeBoard,
  ...MiniApp,
  // App,

  {
    path: '/draw',
    name: 'draw',
    meta: {name: '深部位移'},
    component: () => import(/* webpackChunkName: "home" */'@/views/monitor/monitor-point/monitor-item/draw-echart')
  },
  {
    path: '/openIFrame',
    name: 'openIFrame',
    meta: {name: 'BIM'},
    component: () => import(/* webpackChunkName: "project-list" */'@/views/open_iframe/openIFrame'),
  },
  {
    path: '/appBind',
    meta: {name: '北京久感科技有限公司'},
    name: 'appBind',
    component: () => import(/* webpackChunkName: "login" */'@/views/official-account/bind')
  },
  {
    path: '/home',
    name: 'equipInfo',
    meta: {name: '北京久感科技有限公司'},
    component: () => import(/* webpackChunkName: "home" */'@/views/device_mgmt/jgkj/equipInfo')
  },
  // 设备详情,
  {
    path: '/equipInfo',
    meta: {name: '设备详情'},
    name: 'equipInfo',
    component: () => import(/* webpackChunkName: "login" */'@/views/device_mgmt/jgkj/equipInfo')
  },
  {
    path: '/video',
    meta: {name: '视频'},
    name: 'video',
    component: () => import(/* webpackChunkName: "login" */'@/views/device_mgmt/videoSingle')
  },
  {
    path: '/login',
    meta: {name: '登录'},
    name: 'login',
    // 交通地灾云
    component: () => import(/* webpackChunkName: "login" */'@/views/login/index')
  },
  {
    path: '/register',
    name: 'register',
    meta: {name: '注册'},
    component: () => import('@/views/login/register.vue')
  },
  {
    path: '/forget',
    name: 'forget',
    meta: {name: '密码找回'},
    component: () => import('@/views/login/forget.vue')
  },
  {
    path: '/service',
    name: 'service',
    meta: {name: '服务条款'},
    component: () => import('@/views/login/service.vue')
  },
  {
    path: '/modelDemonstration',
    name: 'modelDemonstration',
    meta: {name: '天空之桥'},
    component: () => import('@/views/monitor/modelDemonstration.vue')
  },
  {
    path: '/50X',
    component: () => import(/* webpackChunkName: "50X" */'@/components/error/50X')
  },
  {
    // 404页面
    path: '*',
    component: () => import(/* webpackChunkName: "error" */'@/components/error/404')
  },
]

// 将路由封装在一个异步函数中，方便重置路由
const createRouter = () => new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: constantRouterMap
})

const router = createRouter()
export default router

// 初始化路由，用于清除手动挂载的 frameworkRouter（asyncRouterMap权限路由将挂载在frameworkRouter下）
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

// 管理后台布局路由总渲染口，所有权限路由都挂载在其中
export const frameworkRouter = {
  // 示例
  path: '/',
  redirect: '/test',
  meta: {name: '管理系统'},
  component: () => import(/* webpackChunkName: "error" */'@/views/layout'),
  children: []
}

// 最终挂载到frameworkRouter => children的白名单路由 即在 @/views/layout 框架下渲染
export const layoutWhiteRouter = [
  {
    path: '/403',
    component: () => import(/* webpackChunkName: "403" */'@/components/error/403')
  },
  {
    // 404页面
    path: '*',
    component: () => import(/* webpackChunkName: "error" */'@/components/error/404')
  },
]

router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  if (isChunkLoadFailed) {
    window.location.reload();
    // router.replace(router.history.pending.fullPath);
  } else {
  }
});


// 路由白名单，权限之外
let WHITE_LIST = ['/modelDemonstration','/openIFrame','/draw', '/appBind', '/video', '/1Mini', '/2Mini', '/3Mini', '/4Mini', '/5Mini', '/7Mini', '/8Mini','/15Mini','/18Mini', '/login', '/register', '/home', '/equipInfo', '/50X', '/demo', '/layout', '/forget']
WHITE_LIST = [...WHITE_LIST, ...layoutWhiteRouter.map(x => x.path)]
export {WHITE_LIST}
